<?php

namespace app\models;

use app\models\searchs\ClientFilterInterface;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\AttrName;
use yii\data\DataFilter;

/**
 * AttrNameSearch represents the model behind the search form of `app\models\AttrName`.
 */
class AttrNameSearch extends AttrName implements ClientFilterInterface

{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'classify_id'], 'integer'],
            [['name'], 'safe'],
        ];
    }

    public function filters()
    {
        return [
            'name' => ['like' => ''],
            'classify_id' => ['eq' => 0],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'Id'),
            'name' => Yii::t('app', 'Name'),
            'classify_id' => Yii::t('app', 'Classify Id'),
        ];
    }

    public function attributeTypes()
    {
        return [
            'id' => DataFilter::TYPE_INTEGER,
            'name' => DataFilter::TYPE_STRING,
            'classify_id' => DataFilter::TYPE_INTEGER,
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = AttrName::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'classify_id' => $this->classify_id,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name]);

        return $dataProvider;
    }
}
